
package com.joysuch.wwyt.bp.repository;

import com.joysuch.wwyt.bp.bean.inspect.BpInspectRecordDetailDto;
import com.joysuch.wwyt.bp.entity.inspect.BpInspectRecordDetail;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

import java.util.List;

public interface BpInspectRecordDetailRepository extends JpaRepository<BpInspectRecordDetail, Long> {

    BpInspectRecordDetail findOneByInspectRecordIdAndInspectPlanIdAndInspectPointIdAndInspectDeviceId(
            @Param("inspectRecordId") Long inspectRecordId, @Param("inspectPlanId") Long inspectPlanId,
            @Param("inspectPointId") Long inspectPointId, @Param("inspectDeviceId") Long inspectDeviceId);

    List<BpInspectRecordDetail> findByInspectRecordIdAndInspectPlanIdAndInspectPointId(
            @Param("inspectRecordId") Long inspectRecordId, @Param("inspectPlanId") Long inspectPlanId,
            @Param("inspectPointId") Long inspectPointId);

    List<BpInspectRecordDetail> findByInspectRecordId(Long inspectRecordId);

    List<BpInspectRecordDetail> findByInspectRecordIdIn(List inspectRecordIds);

    @Query("select new com.joysuch.wwyt.bp.bean.inspect.BpInspectRecordDetailDto(d.inspectRecordId,d.inspectResultStatus," +
            "d.repairStatus) from BpInspectRecordDetail d " +
            "where d.inspectRecordId in (:inspectRecordIds)")
    List<BpInspectRecordDetailDto> findResultStatusByInspectRecordIdIn(List<Long> inspectRecordIds);

    @Query("select new com.joysuch.wwyt.bp.bean.inspect.BpInspectRecordDetailDto(d.inspectPointId,d.inspectResultStatus," +
            "d.repairStatus) from BpInspectRecordDetail d " +
            "where d.inspectRecordId in (:inspectRecordIds)")
    List<BpInspectRecordDetailDto> findPointIdAndResultByInspectRecordIdIn(List inspectRecordIds);

    void deleteByInspectRecordId(Long inspectRecordId);

    List<BpInspectRecordDetail> findByInspectPlanIdAndInspectResultStatus(@Param("inspectPlanId") Long inspectPlanId,
                                                                          @Param("inspectResultStatus") String inspectResultStatus);

    Page<BpInspectRecordDetail> findByInspectDeviceId(Long deviceId, Pageable page);

    @Query(value = "select c.name from BpInspectRecordDetail d inner join BpDeviceFacilityCollection c on d.inspectDeviceId = c.id where d.id=:id")
    String getDeviceNameById(Long id);

    @Query(value = "select distinct(d.inspectRecordId) from BpInspectRecordDetail d where d.inspectDeviceId=:inspectDeviceId order by d.inspectRecordId desc")
    List<Long> findRecordIdByInspectDeviceIdOrderByRecordId(Long inspectDeviceId);
}
